package sg.edu.nus.iss.ooad.jass.entity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Properties;

/**
 *
 * Company value object. This class is value object representing database table
 * Company. This class is intended to be used together with associated DAO object.
 * This class enscapsulates a particular Company.
 *
 * @author Isak Rabin (HT082193M)
 * @version 1.0
 * @since 1.0
 *
 * Development Environment : Netbeans IDE v6.7
 * Name of the File        : Company.java
 * Creation / Modification History
 *         Isak Rabin        20-Sep-2009        Created
 *
 */
public class Company implements IJASSEntity, Serializable {

        private static final long serialVersionUID = -2704312684995111600L;
        //Holds the attributes of Company Entity
        public static final String[] COMPANY_ATTRIBUTES = {"ID", "Name", "CompanyAddress", "CompanyContactPerson", "CompanyContactNo", "CompanyEmail", "Visible"};
        /**
         * Persistent Instance variables.
         * This data is directly  mapped to the columns of database table.
         */
        private String id = "";
        private String name = "";
        private String address = "";
        private String contactPerson = "";
        private String contactNo = "";
        private String email = "";
        private String visible = "";
        /**
         * This data holds a reference to another entity (Associations).
         */
        private Collection<JobAdvertisement> jobadvertisement;

        /**
         * Default Contructor for Company Class
         */
        public Company() {
                jobadvertisement = new ArrayList<JobAdvertisement>();
        }

        /**
         * Constuctor of Company Class
         * @param id
         * @param name
         * @param address
         * @param contactperson
         * @param contactno
         * @param email
         * @param visible
         */
        public Company(String id, String name, String address, String contactperson, String contactno, String email, String visible) {
                this();
                this.id = id;
                this.name = name;
                this.address = address;
                this.contactPerson = contactperson;
                this.contactNo = contactno;
                this.email = email;
                this.visible = visible;
        }

        /**
         * Get the value of Id
         * @return the value of Id
         */
        public String getID() {
                return id;
        }

        /**
         * Set the value of Id
         * @param id new value of ID
         */
        public void setID(String id) {
                this.id = id;
        }

        /**
         * Get the value of name
         * @return the value of name
         */
        public String getName() {
                return name;
        }

        /**
         * Set the value of name
         * @param name new value of name
         */
        public void setName(String name) {
                this.name = name;
        }

        /**
         * Get the value of address
         * @return the value of address
         */
        public String getAddress() {
                return address;
        }

        /**
         * Set the value of address
         * @param address new of address
         */
        public void setAddress(String address) {
                this.address = address;
        }

        /**
         * Get the value of Contact Person
         * @return the value of Contact person
         */
        public String getContactPerson() {
                return contactPerson;
        }

        /**
         * Set the value of Contact Person
         * @param contactPerson new value of Contact Person
         */
        public void setContactPerson(String contactPerson) {
                this.contactPerson = contactPerson;
        }

        /**
         * Get the value of Contact No
         * @return the value of Contact No
         */
        public String getContactNo() {
                return contactNo;
        }

        /**
         * Set the value of Contact No
         * @param contactno new value of Contact No
         */
        public void setContactNo(String contactno) {
                this.contactNo = contactno;
        }

        /**
         * Get the value of Email
         * @return the value of Email
         */
        public String getEmail() {
                return email;
        }

        /**
         * Set the value of Email
         * @param email new value of Email
         */
        public void setEmail(String email) {
                this.email = email;
        }

        /**
         * Get the value of Visible
         * @return the value of visible
         */
        public String getVisible() {
                return visible;
        }

        /**
         * Set the value of Visible
         * @param visible new value of visible
         */
        public void setVisible(String visible) {
                this.visible = visible;
        }

        public boolean isVisible()
        {
            if (visible.equals("1"))return true;
            else return false;
        }

        /**
         * Get the value of jobadvertisement
         * @return the value of jobadvertisement
         */
        public Collection<JobAdvertisement> getJobAdvertisement() {
                return jobadvertisement;
        }

        /**
         * Set the value of jobadvertisement
         * @param jobadvertisement new value of jobadvertisement
         */
        public void setJobadvertisement(Collection<JobAdvertisement> jobadvertisement) {
                this.jobadvertisement = jobadvertisement;
        }

        /**
         * Converts Properties to Company Entity
         * @param properties
         * @return Hashtable of Company Entity
         */
        public Hashtable<String, IJASSEntity> fromJASSPropertiesFormat(Properties properties) {
                Hashtable<String, IJASSEntity> companies = new Hashtable<String, IJASSEntity>();

                String sTotalRecords = properties.getProperty("NumOfItems", "0");
                int iTotalRecords = Integer.parseInt(sTotalRecords);
                for (int i = 1; i <= iTotalRecords; i++) {
                        String[] values = new String[COMPANY_ATTRIBUTES.length];
                        for (int j = 0; j < COMPANY_ATTRIBUTES.length; j++) {
                                values[j] = properties.getProperty(COMPANY_ATTRIBUTES[j] + String.valueOf(i));
                        }
                        Company company = new Company(values[0], values[1], values[2], values[3], values[4], values[5], values[6]);
                        companies.put(values[0], company);
                }

                return companies;
        }

        /**
         * Converts Company Entity to Properties Format
         * @param collections
         * @return Properties of Company Entity
         */
        public Properties toJassPropertiesFormat(Collection<IJASSEntity> collections) {
                Properties properties = new Properties();

                int count = 0;
                for (Iterator<IJASSEntity> i = collections.iterator(); i.hasNext();) {
                        count = count + 1;
                        Company company = (Company) i.next();
                        properties.setProperty(COMPANY_ATTRIBUTES[0] + String.valueOf(count), company.getID());
                        properties.setProperty(COMPANY_ATTRIBUTES[1] + String.valueOf(count), company.getName());
                        properties.setProperty(COMPANY_ATTRIBUTES[2] + String.valueOf(count), company.getAddress());
                        properties.setProperty(COMPANY_ATTRIBUTES[3] + String.valueOf(count), company.getContactPerson());
                        properties.setProperty(COMPANY_ATTRIBUTES[3] + String.valueOf(count), company.getContactNo());
                        properties.setProperty(COMPANY_ATTRIBUTES[3] + String.valueOf(count), company.getEmail());
                        properties.setProperty(COMPANY_ATTRIBUTES[3] + String.valueOf(count), company.getVisible());
                }
                properties.setProperty("NumOfItems", String.valueOf(collections.size()));

                return properties;
        }

        @Override
        public String toString() {
                return super.toString();
        }
}
